
/*
 *
 * Copyright (C) 2008 Mattias Blomqvist, patr-blo at dsv dot su dot se
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

using FlowLib.Containers;
using ClientExample.Comparers;
using ClientExample.Converters;
using ClientExample.Filters;
using System.Windows.Forms;
using System;

namespace ClientExample.Controls
{
    public class ListViewUserlist : ListViewExtended<UserInfo>
    {
        public ListViewUserlist()
            : base()
        {
            AllItems.Sort(new UserInfoComparer(UserInfoComparer.Columns.ID));
            Items.Sort(new UserInfoComparer(UserInfoComparer.Columns.DisplayName));
            Converter = new UserInfoConverter();
            Filter = new UserInfoFilter();
            this.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(ListViewUserlist_ColumnClick);

            ColumnHeader headerName = new ColumnHeader();
            headerName.Text = "Name";
            headerName.Tag = UserInfoComparer.Columns.DisplayName.ToString();
            headerName.Width = 102;
            Columns.Add(headerName);

            ColumnHeader headerShare = new ColumnHeader();
            headerShare.Text = "Share";
            headerShare.Tag = UserInfoComparer.Columns.Share.ToString();
            headerShare.Width = 75;
            headerShare.TextAlign = HorizontalAlignment.Right;
            Columns.Add(headerShare);
        }

        void ListViewUserlist_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            UserInfoComparer comp = null;
            UserInfoComparer.Columns column = (UserInfoComparer.Columns)Enum.Parse(typeof(UserInfoComparer.Columns), this.Columns[e.Column].Tag.ToString());
            comp = new UserInfoComparer(column);
            if (((UserInfoComparer)Items.Comparer).SortOnColumn == column)
            {
                comp.Reverse = !((UserInfoComparer)Items.Comparer).Reverse;
            }
            Items.Sort(comp);
			this.Invalidate(true);
        }
    }
}
